import Vue from 'vue'
import App from './App'
import router from './router'
import iView from 'iview'
import axios from 'axios'
import store from './store'
import 'iview/dist/styles/iview.css'
import './common/less/index.less'

import {getLocalStorage} from './lib/util'

Vue.config.productionTip = false
Vue.use(iView)

axios.defaults.withCredentials = true
axios.defaults.headers.Authorization = JSON.parse(getLocalStorage('userInfo')).token || getLocalStorage('token') || ''
Vue.prototype.$http = axios

router.beforeEach((to, from, next) => {
  let user = JSON.parse(getLocalStorage('userInfo')) || null
  if (to.meta.role && to.meta.role.find(role => role === 'admin')) {
    if (user && user.isAdmin) {
      next()
    } else {
      next({path: '/login'})
    }
  } else {
    next()
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
